﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web.Compilation;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.WebPages;
using MvcApplication1;
using PrecompiledMvcViewEngineContrib;

[assembly: WebActivator.PreApplicationStartMethod(typeof(Activate), "Engine")]
namespace MvcApplication1
{
    
    
    public class Activate
    {
         public static void Engine()
         {
             var assemblies = new List<Assembly>();
             var current = Assembly.GetAssembly(typeof (MvcApplication));
             assemblies.Add(current);
            
             foreach(var file in Directory.EnumerateFiles(HostingEnvironment.MapPath(@"~\Compiled"),"*.dll"))
             {
                 try
                 {
                     var a = Assembly.Load(AssemblyName.GetAssemblyName(file));
                     assemblies.Add(a);
                     BuildManager.AddReferencedAssembly(a);
                 }
                 catch
                 {
                     
                 }
             }
            
             var engine = new PrecompiledMvcEngine(assemblies);

             ViewEngines.Engines.Insert(0, engine);

             // StartPage lookups are done by WebPages. 
             VirtualPathFactoryManager.RegisterVirtualPathFactory(engine);  

            
         }

    }
}